﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Configuration;
using Communergy.Repository;
using Communergy.Common;
using Microsoft.Practices.Unity;
using CommunergyLogic;
using Communergy.Data;
using Communergy.Repository.EntityFramework;
using System.ServiceModel.Activation;

namespace CommunergyWebApplication.services
{
    // NOTE: If you change the class Name "CommunergyUploadService" here, you must also update the reference to "CommunergyUploadService" in Web.config.
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class CommunergyUploadService : IRealtimeRepository, IHistoricRepository, IMeterRepository
    {
        DataAccessLayer dataAccess;

        public CommunergyUploadService()
        {
            IRepositoryFactory repositoryFactory = new EntityRepositoryFactory();
            dataAccess = new DataAccessLayer(repositoryFactory);

            //using (IUnityContainer container = new UnityContainer())
            //{
            //    container.RegisterType<IRepositoryFactory, EntityRepositoryFactory>();
            //    dataAccess = container.Resolve<DataAccessLayer>();
                
            //}
        }

        #region ICommunergyUploadService Members

        public void Insert_ElectricityRealtime(string meterId,  List<Communergy.Common.RealtimeEnergyReading> realtimeEnergyReadings)
        {

            dataAccess.Insert_ElectricityRealtime(meterId, realtimeEnergyReadings);

        }

        #endregion

        #region ICommunergyUploadService Members


        public void Insert_ElectricityHourly(string meterId,  List<Communergy.Common.HistoricalEnergyReading> hourlyEnergyReadings)
        {

            dataAccess.Insert_ElectricityHourly(meterId, hourlyEnergyReadings);
        }

        public void Insert_ElectricityDaily(string meterId,  List<Communergy.Common.HistoricalEnergyReading> dailyEnergyReadings)
        {
   
            dataAccess.Insert_ElectricityDaily(meterId, dailyEnergyReadings);
        }

        public void Insert_ElectricityMonthly(string meterId,  List<Communergy.Common.HistoricalEnergyReading> monthlyEnergyReadings)
        {

            dataAccess.Insert_ElectricityMonthly(meterId, monthlyEnergyReadings);
        }

        #endregion

        #region IRealtimeRepository Members


        public List<RealtimeEnergyReading> Get_ElectricityRealtime(string meterId, DateTime dateFrom, DateTime dateTo)
        {

            //IRealtimeRepository repository = EntityRepositoryFactory.CreateRealtimeRepository();
            //var realtime = repository.Get_ElectricityRealtime(meterId, dateFrom, dateTo);

            var realtime = dataAccess.Get_ElectricityRealtime(meterId, dateFrom, dateTo);
            return realtime;
        }

     

        public RealtimeEnergyReading GetLastElectricityRealtime(string meterId)
        {
            //IRealtimeRepository repository = EntityRepositoryFactory.CreateRealtimeRepository();
            var realtime = dataAccess.GetLastElectricityRealtime(meterId);
            return realtime;
        }

        #endregion

        #region IMeterRepository Members

        public Meter CreateNewMeter(Meter meterToCreate)
        {
            return dataAccess.CreateNewMeter(meterToCreate);

            //IMeterRepository repository = EntityRepositoryFactory.CreateMeterRepository();
            //var newMeter = repository.CreateNewMeter(meterToCreate);           
            //return newMeter;
        }

        public Meter RetrieveMeterDetails(string meterId)
        {
            throw new NotImplementedException();
        }

        public void SaveMeterDetails(Meter meterToSave)
        {
            throw new NotImplementedException();
        }

        public List<Meter> GetMetersForUser(string userId)
        {
            return dataAccess.GetMetersForUser(userId);
            //IMeterRepository repository = EntityRepositoryFactory.CreateMeterRepository();
            //var meters = repository.GetMetersForUser(userId);
            //return meters;
        }

        #endregion


        //TODO - reporter
        public List<HistoricalEnergyReading> GetHourly(string meterId, DateTime dateFrom)
        {
            throw new NotImplementedException();
        }

        #region IRealtimeRepository Members


        public HistoricalEnergyReading GetCumulativeElectricityRealtime(string meterId, DateTime dateFrom, DateTime dateTo)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
